Public Class TryGame
Inherits Microsoft.Xna.Framework.Game
Private WithEvents graphics As GraphicsDeviceManager
Private WithEvents spriteBatch As SpriteBatch
Private BatSpeed As Single = 3
Private BallSpeed As Single = 3
Private Bat As GameObject
Private Ball As GameObject
#Region "My methods"
Private Function GetTexture(ByVal Name As String) As Texture2D
Dim ImageStream As New System.IO.FileStream(My.Application.Info.DirectoryPath & "\" & Name, IO.FileMode.Open)
Return Texture2D.FromStream(Me.graphics.GraphicsDevice, ImageStream)
End Function
Private Sub KeyboardCheck()
Dim KeyState As KeyboardState = Keyboard.GetState()
If KeyState.IsKeyDown(Keys.Right) Then
Bat.Position += New Vector2(BatSpeed, 0)
End If
If KeyState.IsKeyDown(Keys.Left) Then
Bat.Position += New Vector2(-BatSpeed, 0)
End If
If KeyState.IsKeyDown(Keys.Escape) Then
Me.Exit()
End If
End Sub
Private Sub CollisionCheck()
Dim BatRect As Rectangle = New Rectangle(Bat.Position.X - (Bat.Sprite.Width / 2), Bat.Position.Y - (Bat.Sprite.Width / 2), Bat.Sprite.Width, Bat.Sprite.Height)
Dim BallRect As Rectangle = New Rectangle(Ball.Position.X - (Ball.Sprite.Width / 2), Ball.Position.Y - (Ball.Sprite.Height / 2), Ball.Sprite.Width, Ball.Sprite.Height)
If BallRect.Intersects(BatRect) Then
Ball.Velocity = New Vector2(Ball.Velocity.X, -Ball.Velocity.Y)
End If
If ((Ball.Position.X > 5) Or (Ball.Position.X > Me.GraphicsDevice.Viewport.Width - 5)) And (Ball.Position.Y < Me.GraphicsDevice.Viewport.Height - 5) Then
Ball.Velocity += New Vector2(Ball.Position.X, -Ball.Position.Y)
End If
End Sub
#End Region
Public Sub New()
graphics = New GraphicsDeviceManager(Me)
Content.RootDirectory = "Content"
End Sub
Protected Overrides Sub Initialize()
MyBase.Initialize()
spriteBatch = New SpriteBatch(GraphicsDevice)
End Sub
Protected Overrides Sub LoadContent()
MyBase.LoadContent()
Bat = New GameObject(GetTexture("baton.png"))
Bat.Position = New Vector2(Me.GraphicsDevice.Viewport.Width / 2, Me.GraphicsDevice.Viewport.Height - 40)
Ball = New GameObject(GetTexture("ball.png"))
Ball.Position = New Vector2(Me.GraphicsDevice.Viewport.Width / 2, Me.GraphicsDevice.Viewport.Height / 2)
Ball.Velocity = New Vector2(0, BallSpeed)
End Sub
Protected Overrides Sub UnloadContent()
MyBase.UnloadContent()
End Sub
Protected Overrides Sub Update(ByVal gameTime As GameTime)
KeyboardCheck()
CollisionCheck()
Ball.Position += Ball.Velocity
If GamePad.GetState(PlayerIndex.One).Buttons.Back = ButtonState.Pressed Then
Me.Exit()
End If
MyBase.Update(gameTime)
End Sub
Protected Overrides Sub Draw(ByVal gameTime As GameTime)
GraphicsDevice.Clear(Color.Blue)
spriteBatch.Begin()
Bat.Draw(spriteBatch)
Ball.Draw(spriteBatch)
spriteBatch.End()
MyBase.Draw(gameTime)
End Sub
End Class